// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_detail_info = require("./financingDetailInfo.css");
	var js_detail_info = require("./financingDetailInfo.js");
	var html_financeWaybill = require("./html_financeWaybill.html");
	var css_financingDetailEvidentiaryCategorys = require("./financingDetailEvidentiaryCategorys.css");
	var tools = require("tools");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var financeId = "";
	var fileName;
	var dataFinancingDetail;
	var init = function(page) {

		financeId = page.params.financeId || base.getUrlParam("financeId");
		initEvent();
		js_detail_info.init(page);
		showEvidentiaryCategorys();
		router.init();
		initCss();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//点击显示提单号
		$(".platformBtns").on("click", ".button-submit-financeWaybill", function() {
			$("#popdialog").html(html_financeWaybill);
			dialog.showDialog($("#financeWaybill_dialog"), function() {});
			databind.fillForm($("#financeWaybill_dialog"), {
				financeId: financeId
			});
			inquireFinanceWaybillList();
		});
	};

	//查询融资信息的提单号
	var inquireFinanceWaybillList = function() {
		base.fnAjax({
			action: "inquireFinanceWaybillList.json",
			version: "v1.0",
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					var tpl;

					if (data.datas.length > 0) {

						tpl = $("#WaybillContent1").html();
						var html = juicer(tpl, data);
						$(".WaybillContent").html(html);
					} else {
						tpl = $("#WaybillContent").html();
						$(".WaybillContent").html(tpl);
					}

					databind.fillForm($(".WaybillContent"), {
						bookingSpaceNumber: dataFinancingDetail.datas[0].bookingSpaceNumber
					});
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	var initCss = function() {
		//  	$(".d2").css("background","url(../../../res/png/tab_bg2.png) 72% 100% no-repeat");
		$(".financeDetailInfo").css("color", "#000000");
		$(".evidentiaryCategorys").css("color", "#FF7344");
	}
	var showEvidentiaryCategorys = function() {
		base.fnAjax({
			action: "inquireFinanceApplicationDetail.json",
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas[0].evidentiaryCategorys.length == 0) {
						$("#evidentiaryCategorysBody").html("暂无数据");
					} else {
						dataFinancingDetail = data;
						var tpl = $("#evidentiaryCategorysTpl").html();
						var html = juicer(tpl, data);
						$("#evidentiaryCategorysBody").html(html);
						var materialItems = $(".materialItem .d11");
						for (var j = 0; j < materialItems.length; j++) {
							$(materialItems[j]).find(" span").html(tools.inquireFile($(materialItems[j]).attr("materialFileuuid")));
							for (var k = materialItems.length - j + 1; k > 1; k--) {
								if ($(materialItems[k]).attr("templateId") == $(materialItems[k - 1]).attr("templateId")) {
									$(materialItems[k]).find(".d1").clone().appendTo($(materialItems[k - 1]));
									$(materialItems[k]).remove();
									k = 1;
								}
							}
						}
					}
					
					var i = 1;
					$("#financingDetailEvidentiaryCategorys .financeContract").html("<tr><th><input type='checkbox' class='checkAll none'></th><th>No</th><th>文件名称</th><th>文件类型</th><th>状态</th><th class='none'>签署记录</th><th class='none'>退回原因</th><th>操作</th></tr>")
					$.each(data.datas[0].financeContractDetail.financeFilesInfoList, function() {
						var isSign = this.isSign;
						var filesInfoId = this.filesInfoId;
						var signState = this.fileSignState.split(',');
						var msg = [];
						if (this.msg != null) {
							msg = this.msg.split(',');
						}
						var j = 0;
						var fs1 = this.filesUuid.split(',');
						var ss1 = this.isSign.split(',');
						for (var x = 0; x < fs1.length; x++) {
							var _this = fs1[x];
							if ($.trim(_this) != "") {
								var m = msg[j];
								if (m == null || m == "null") {
									m = "";
								}
								var str;
								if (ss1[x] == '1') {
									str = "<tr signState='"+signState[j]+"' fileUuid='"+_this+"' filesInfoId='"+filesInfoId+"' ><td><input checked='checked' type='checkbox' class='checks none'></td><td>"+i+"</td><td>"+tools.inquireFile(_this)+"</td><td>签署文件</td><td>"+constant.getValueByKey(73, signState[j])+"</td><td class='none'><a href='javascript:void(0);' class='showSignRecord'>查看</td><td class='none'>"+m+"</td><td><a target='_blank' href='/finance/api/v1.0/viewPdfFile.json?fileUuid="+_this+"'>预览</a></td></tr>";
								} else {
									str = "<tr signState='"+signState[j]+"' fileUuid='"+_this+"' filesInfoId='"+filesInfoId+"' ><td><input type='checkbox' disabled='disabled' class='none'></td><td>"+i+"</td><td>"+tools.inquireFile(_this)+"</td><td>附件</td><td>-</td><td class='none'></td><td><a target='_blank' href='/finance/api/v1.0/viewPdfFile.json?fileUuid="+_this+"'>预览</a></td></tr>";
								}
								if (str != null) {
									$("#financingDetailEvidentiaryCategorys .financeContract").append(str);
									i++;
									str = null;
								}
								m = "";
								j++;
							}
						};
					});
					
					i = 1;
					$("#financingDetailEvidentiaryCategorys .supplementContract").html("<tr><th><input type='checkbox' class='checkAll none'></th><th>No</th><th>文件名称</th><th>文件类型</th><th>状态</th><th class='none'>签署记录</th><th class='none'>退回原因</th><th>操作</th></tr>")
					$.each(data.datas[0].supplementContractDetail.financeFilesInfoList, function() {
						var isSign = this.isSign;
						var filesInfoId = this.filesInfoId;
						var signState = this.fileSignState.split(',');
						var msg = [];
						if (this.msg != null) {
							msg = this.msg.split(',');
						}
						var j = 0;
						var fs1 = this.filesUuid.split(',');
						var ss1 = this.isSign.split(',');
						for (var x = 0; x < fs1.length; x++) {
							var _this = fs1[x];
							if ($.trim(_this) != "") {
								var m = msg[j];
								if (m == null || m == "null") {
									m = "";
								}
								var str;
								if (ss1[x] == '1') {
									str = "<tr signState='"+signState[j]+"' fileUuid='"+_this+"' filesInfoId='"+filesInfoId+"' ><td><input checked='checked' type='checkbox' class='checks none'></td><td>"+i+"</td><td>"+tools.inquireFile(_this)+"</td><td>签署文件</td><td>"+constant.getValueByKey(73, signState[j])+"</td><td class='none'><a href='javascript:void(0);' class='showSignRecord'>查看</td><td class='none'>"+m+"</td><td><a target='_blank' href='/finance/api/v1.0/viewPdfFile.json?fileUuid="+_this+"'>预览</a></td></tr>";
								} else {
									str = "<tr signState='"+signState[j]+"' fileUuid='"+_this+"' filesInfoId='"+filesInfoId+"' ><td><input type='checkbox' disabled='disabled' class='none'></td><td>"+i+"</td><td>"+tools.inquireFile(_this)+"</td><td>附件</td><td>-</td><td class='none'></td><td><a target='_blank' href='/finance/api/v1.0/viewPdfFile.json?fileUuid="+_this+"'>预览</a></td></tr>";
								}
								if (str != null) {
									$("#financingDetailEvidentiaryCategorys .supplementContract").append(str);
									i++;
									str = null;
								}
								m = "";
								j++;
							}
						};
					});
				} else {
					base.alert(3, data.message);
				}
			}
		});

	}

	return {
		init: init
	};
});